import json
import os
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QLabel, QStyledItemDelegate, QGridLayout, QHeaderView, QFileDialog, QComboBox, QFrame, QGroupBox, QAbstractItemView, QTableWidgetItem, QTableWidget, QSpacerItem, QSizePolicy, QWidget
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QEvent
from PyQt5.QtGui import QPainter, QPen, QColor, QDoubleValidator
from PyQt5.Qt import Qt, QRect, QCompleter, QSortFilterProxyModel
from pydantic import BaseModel, Field, field_validator
import gui.controls.envs as envs
from ..basic_grid_component import BasicGridComponent
# import gui.controls.envs as envs
from ..dlg_model_params import DlgModelParams
from ..c_params_table import CParamsTable
from ..dlg_params import DlgParams
from ..utils import Utils
from ..custom_widget import LineEditFloat, LineEditStr, LineEditInt


from enum import Enum


# 带搜索功能的下拉框
class ExtendedComboBox(QComboBox):
    def __init__(self, parent=None):
        super(ExtendedComboBox, self).__init__(parent)
        # self.setFocusPolicy(Qt.StrongFocus)
        self.setEditable(True)
 
        # 添加筛选器模型来筛选匹配项
        self.pFilterModel = QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)  # 大小写不敏感
        self.pFilterModel.setSourceModel(self.model())
 
        # 添加一个使用筛选器模型的QCompleter
        self.completer = QCompleter(self.pFilterModel, self)
        # 始终显示所有(过滤后的)补全结果
        self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)  # 不区分大小写
        self.setCompleter(self.completer)
 
        # Qcombobox编辑栏文本变化时对应的槽函数
        self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
        self.completer.activated.connect(self.on_completer_activated)
 
    # 当在Qcompleter列表选中候，下拉框项目列表选择相应的子项目
    def on_completer_activated(self, text):
        if text:
            index = self.findText(text)
            self.setCurrentIndex(index)
            # self.activated[str].emit(self.itemText(index))
 
    # 在模型更改时，更新过滤器和补全器的模型
    def setModel(self, model):
        super(ExtendedComboBox, self).setModel(model)
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)
 
    # 在模型列更改时，更新过滤器和补全器的模型列
    def setModelColumn(self, column):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)
        super(ExtendedComboBox, self).setModelColumn(column)
 
    # 回应回车按钮事件
    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Enter & e.key() == Qt.Key_Return:
            text = self.currentText()
            index = self.findText(text, Qt.MatchExactly | Qt.MatchCaseSensitive)
            self.setCurrentIndex(index)
            self.hidePopup()
            super(ExtendedComboBox, self).keyPressEvent(e)
        else:
            super(ExtendedComboBox, self).keyPressEvent(e)
  

            
class SelectBox(ExtendedComboBox):
    def __init__(self, parent=None, options=[], flag="", index=0, onChanged=None):
        super().__init__()
        
        
        self.parent = parent
        self.index = index
        self.flag = flag
        # self.setMaximumWidth(200)
        # self._data = data
        # self.setObjectName("value_"+data.get("name"))
        self.onChangedEvent = onChanged
        self._init_show_items(options)
        # self._set(data.get("value"))
        # self.setFixedHeight(WIDGET_HEIGHT)

    def setValue(self, value: str = None):
        self._set(value)

    def getValue(self):
        return self._get()

    def _get(self):
        return self.itemData(self.currentIndex()) or None

    def _set(self, value: str = None):
        if not value:
            self.setCurrentIndex(-1)
        else:
            for index in range(self.count()):
                if self.itemData(index) == value:
                    self.setCurrentIndex(index)
                    break

    def _init_show_items(self, data):
        self.clear()
        items = []
        if isinstance(data, list):
            items = data
        elif isinstance(data, type(lambda x: x)):
            items = data()

        for item in items:
            self.addItem(item.get("title", ""), item.get("value"))
        self.setCurrentIndex(-1)
        self.currentIndexChanged.connect(self.onChanged)
        self.placeholderText = "请选择选项"
        
    def updateItem(self, data):
        items = []
        if isinstance(data, list):
            items = data
        elif isinstance(data, type(lambda x: x)):
            items = data()
        self.clear()
        for item in items:
            self.addItem(item.get("title", ""), item.get("value"))

    def onChanged(self):
        if self.onChangedEvent:
            self.onChangedEvent(self.currentText(), self.index, self.flag)
            

class EventFilter(QObject):
    def eventFilter(self, event):
        # 检查事件是否为按键事件，并且按键是否为回车键
        if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Return:
            # 返回 True 表示事件已被处理，不再传播
            return True
        # 返回 False 表示事件未被处理，可以继续传播
        return False

class FloatValidatorDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super(FloatValidatorDelegate, self).__init__(parent)
        self.validator = QDoubleValidator(self)

    def createEditor(self, parent, option, index):
        editor = super(FloatValidatorDelegate, self).createEditor(parent, option, index)
        editor.setValidator(self.validator)
        return editor

class FineLine(QFrame):
    def __init__(self, parent=None):
        super(FineLine, self).__init__(parent)
        self.setFrameShape(QFrame.HLine)
        self.setFrameShadow(QFrame.Sunken)

    def paintEvent(self, event):
        painter = QPainter(self)
        color = QColor(255, 255, 255, 127)  # 透明度范围0-255，127为半透明

        painter.setPen(QPen(color, 0.5))  # 设置画笔宽度为0.5像素
        painter.drawLine(0, self.height() / 2, self.width(), self.height() / 2)

class CustomTable(CParamsTable):
    
    def __init__(self, rows=0, columns=2, parent=None):
        super(CustomTable, self).__init__(rows=rows, columns=columns, parent=parent)
        self.setEditTriggers(QAbstractItemView.DoubleClicked)
        self.verticalHeader().setVisible(True)


class DlgMaterialSetting(DlgModelParams):
    materialSetting_save_signal = pyqtSignal(object)
    def __init__(self, parent=None):
        super(DlgMaterialSetting, self).__init__(parent)
        self.setWindowTitle('材料属性设置')
        
        self.widgetDict = {}
        
        # self.pBtnOK.setFocusPolicy(Qt.NoFocus)
        # self.pBtnCancel.setFocusPolicy(Qt.NoFocus)
        self.setMinimumWidth(800)
        
        
        self.paramsModeingData = Utils.read_json("data/ipimas/uis/ParamsModeling.json")
            
        materialSettingData = Utils.read_json("data/ipimas/uis/MaterialSetting.json")
        

        self.compositePlyData = []
        self.adhesiveLayerData = []
        self.honeycombCoreData = []
        compositePlyData = Utils.read_json("data/ipimas/uis/compositePly.json")
        if compositePlyData:
            self.compositePlyData = compositePlyData.get("data")
            

        adhesiveLayerData = Utils.read_json("data/ipimas/uis/adhesiveLayer.json")
        if adhesiveLayerData:
            self.adhesiveLayerData = adhesiveLayerData.get("data")

        honeycombCoreData = Utils.read_json("data/ipimas/uis/honeycombCore.json")
        if honeycombCoreData:
            self.honeycombCoreData = honeycombCoreData.get("data")

        self.materialList = transform(self.compositePlyData,self.honeycombCoreData,self.adhesiveLayerData)
        
        
        self.type = ["复合材料单层板","蜂窝芯子","胶层"]
    
        topPlyLayers = self.createLayersTable("上铺层", "topPly", 1)
        bottomPlyLayers = self.createLayersTable("下铺层", "bottomPly", 1)
        topRepairPlyLayers = self.createLayersTable("上铺层修补层", "topRepairPly", 1)
        bottomRepairPlyLayers = self.createLayersTable("下铺层修补层", "bottomRepairPly", 1)
        bottomFillPlyLayers = self.createLayersTable("下铺层填充层", "bottomFillPly", 1)

        
        groupBox1 = QGroupBox("蜂窝芯子")
        vLayout1 = QVBoxLayout()
        subGridLayout = QGridLayout()

        lab1_2 = QLabel("材料")
        self.widgetDict['honeycombCoreMateial'] = SelectBox(options=self.materialList)
        groupBox1.setSizePolicy(QSizePolicy.Ignored, -1)

        subGridLayout.addWidget(lab1_2,1,0)
        subGridLayout.addWidget(self.widgetDict['honeycombCoreMateial'],1,1)
        vLayout1.addLayout(subGridLayout)
        groupBox1.setLayout(vLayout1)
        groupBox2 = QGroupBox("蜂窝芯子修补层")
        vLayout2 = QVBoxLayout()
        subGridLayout2 = QGridLayout()
        lab2_2 = QLabel("材料")
        self.widgetDict['honeycombCoreRepairMateial'] = SelectBox(options=self.materialList)
        groupBox2.setSizePolicy(QSizePolicy.Ignored, -1)


        subGridLayout2.addWidget(lab2_2,1,0)
        subGridLayout2.addWidget(self.widgetDict['honeycombCoreRepairMateial'],1,1)
        vLayout2.addLayout(subGridLayout2)
        groupBox2.setLayout(vLayout2)
        vLayout = QVBoxLayout()
        vLayout.addWidget(groupBox1)
        vLayout.addWidget(groupBox2)


        gridLayout = QGridLayout()
        gridLayout.addWidget(topPlyLayers,1,0)
        gridLayout.addWidget(bottomPlyLayers,1,1)
        gridLayout.addWidget(topRepairPlyLayers,2,0)
        gridLayout.addWidget(bottomRepairPlyLayers,2,1)
        gridLayout.addLayout(vLayout,3,0)
        gridLayout.addWidget(bottomFillPlyLayers,3,1)

        
        
        self.vbox.insertLayout(0, gridLayout)
        
        self.update_UI(self.paramsModeingData, materialSettingData)
        
        envs.ipimas_updateMaterialSetting = lambda type, *args, **kwargs: self.__update_model(type, *args, **kwargs)
        
    def __update_model(self, type, *args, **kwargs):
        if type == "参数化建模":
            
            self.paramsModeingData = Utils.read_json("data/ipimas/uis/ParamsModeling.json")
            
            if (self.paramsModeingData.get('damageType') != "贯穿式损伤" and self.paramsModeingData.get('damageType') != "蜂窝芯子损伤"):
                self.widgetDict["honeycombCoreRepairMateial"].setDisabled(True)
            else:
                self.widgetDict["honeycombCoreRepairMateial"].setDisabled(False)
                
            for v, tableData in self.paramsModeingData.items():
                if v == "topPlyTable" or v == "bottomPlyTable" or v == "topRepairPlyTable" or v == "bottomRepairPlyTable" or v == "bottomFillPlyTable":
                    table = self.widgetDict[v]
                    if v == "bottomFillPlyTable":
                        table = self.widgetDict["bottomPlyTable"]
                    try:
                        num_rows = int(len(tableData))
                        table.setRowCount(num_rows)
                        table.set_columns_editable()
                    except ValueError:
                        table.setRowCount(0)
                        table.set_columns_editable()
                    for row in range(table.rowCount()):
                        for column in range(table.columnCount()):
                            if column == 0:
                                wrapWidget = table.cellWidget(row, column)
                                if not wrapWidget:
                                    combo = SelectBox(options=self.materialList)
                                    layout = QVBoxLayout()
                                    layout.addWidget(combo)
                                    cell_widget = QWidget()
                                    cell_widget.setLayout(layout)
                                    table.setCellWidget(row, column, cell_widget)
                    table.resizeRowsToContents()
                    
            if "bottomRepairPlyTable" not in self.paramsModeingData:
                self.widgetDict["bottomRepairPlyTable"].setRowCount(0)
            
        
    def update_UI(self, data, fillData):
        if (self.paramsModeingData.get('damageType') != "贯穿式损伤" and self.paramsModeingData.get('damageType') != "蜂窝芯子损伤"):
            self.widgetDict["honeycombCoreRepairMateial"].setDisabled(True)
        
        for v, tableData in data.items():
            if v == "topPlyTable" or v == "bottomPlyTable" or v == "topRepairPlyTable" or v == "bottomRepairPlyTable":
                self.createTableRow(v, tableData)
            if v == "bottomPlyTable":
                self.createTableRow('bottomFillPlyTable', tableData)
        
        
        for v, tableData in fillData.items():
            if v == 'honeycombCore' or v == 'honeycombCoreRepair':
                self.widgetDict[f'{v}Mateial'].setValue(tableData.get('mateial'))
            else:
                self.fillTableData(v, tableData)
                
        
                            
                            
    def createTableRow(self, v, tableData):
        table = self.widgetDict[v]
        table.setColumnCount(2)
        table.setRowCount(len(tableData))
        for rowIdx, rowData in enumerate(tableData):
            for colIdx in range(2):
                if colIdx == 0:
                    combo = SelectBox(options=self.materialList)
                    layout = QVBoxLayout()
                    layout.addWidget(combo)
                    cell_widget = QWidget()
                    cell_widget.setLayout(layout)
                    table.setCellWidget(rowIdx, colIdx, cell_widget)  
                else:
                    table.setItem(rowIdx, colIdx, QTableWidgetItem(''))
            
            table.resizeRowsToContents()
                    
    def fillTableData(self, v, data):
        table = self.widgetDict[v]
        for row in range(table.rowCount()):
            if len(data)-1 < row:
                continue
            for column in range(table.columnCount()):
                if column == 0:
                    wrapWidget = table.cellWidget(row, column)
                    wrapLayout = wrapWidget.layout()
                    selectBox = wrapLayout.itemAt(0).widget()
                    selectBox.setValue(data[row].get('material'))
                else: 
                    item = QTableWidgetItem(str(data[row].get('Angle')))
                    item.setTextAlignment(Qt.AlignCenter)
                    table.setItem(row, column, item)
                
    def createLayersTable(self, title, name, type=0):
        groupBox = QGroupBox(title)
        layersLayout = QVBoxLayout()
        if type == 1:
            table = CustomTable(rows=0, columns=2)
            table.setItemDelegateForColumn(1, FloatValidatorDelegate(table))
            table.setHorizontalHeaderLabels(["材料", "铺层角度"])
            table.horizontalHeader().setSectionResizeMode(
                1, QHeaderView.Fixed
            )  # 第一列固定
            table.setColumnWidth(1, 100)
        else:
            table = CustomTable(rows=0, columns=2)
            table.setMaximumHeight(90)
            # table.setItemDelegateForColumn(0, FloatValidatorDelegate(table))
            table.setHorizontalHeaderLabels(["材料"])
        self.widgetDict[f'{name}Table'] = table
        layersLayout.addWidget(table)
        groupBox.setLayout(layersLayout)

        return groupBox

    # def getTableDict(self, table, valueList, descriptionList, type):
    #     for i in range(table.rowCount()):
    #         rowDict = {}
    #         typeList = ["铺层名称", "厚度"]
    #         for j in range(table.columnCount()):
                
    #             item = table.item(i, j)
    #             if type == 1:
    #                 if j == 1:
    #                     rowDict[typeList[j]] = float(item.text()) if item.text() is not None else None
    #                 else:
    #                     rowDict[typeList[j]] = item.text()
    #             elif type == 0:
    #                 rowDict['厚度'] = float(item.text()) if item.text() is not None else None
    #         valueList.append(rowDict)
    #         descriptionList.append(rowDict)
    #     return valueList
    
    def changedType(self, value, flag, idx, *args, **kwargs):
        ["复合材料单层板","蜂窝芯子","胶层"]
        wrapWidget = self.widgetDict[flag].cellWidget(idx, 1)
        wrapLayout = wrapWidget.layout()
        selectBox = wrapLayout.itemAt(0).widget()
        if value == "复合材料单层板":
            selectBox.updateItem(self.compositePlyOptions)
        elif value == "蜂窝芯子":
            selectBox.updateItem(self.honeycombCoreOptions)
        elif value == "胶层":
            selectBox.updateItem(self.adhesiveLayerOptions)

    @pyqtSlot()
    def on_pBtnOK_clicked(self):
        data = {}
        descriptData = {}
        [data['topPlyTable'], descriptData['上铺层']] = self.getTableValue(self.widgetDict['topPlyTable'])
        [data['bottomPlyTable'], descriptData['下铺层']] = self.getTableValue(self.widgetDict['bottomPlyTable'])
        [data['topRepairPlyTable'], descriptData['上铺层修补层']] = self.getTableValue(self.widgetDict['topRepairPlyTable'])
        [data['bottomRepairPlyTable'], descriptData['下铺层修补层']] = self.getTableValue(self.widgetDict['bottomRepairPlyTable'])
        [data['bottomFillPlyTable'], descriptData['下铺层填充层']] = self.getTableValue(self.widgetDict['bottomFillPlyTable'])
        data['honeycombCore'] = {"mateial":self.widgetDict['honeycombCoreMateial'].getValue()}
        data['honeycombCoreRepair'] = {"mateial":self.widgetDict['honeycombCoreRepairMateial'].getValue()}
        # descriptData['蜂窝芯子材料'] = {"材料":self.widgetDict['honeycombCoreMateial'].getValue()}
        # descriptData['蜂窝芯子修补层'] = {"材料":self.widgetDict['honeycombCoreRepairMateial'].getValue()}
        
        descriptData['蜂窝芯子材料'] = {"材料":self.widgetDict['honeycombCoreMateial'].getValue()}
        descriptData['蜂窝芯子修补层'] = {"材料":self.widgetDict['honeycombCoreRepairMateial'].getValue()}
        

        if self.paramsModeingData.get('damageType') != "贯穿式损伤" and  self.paramsModeingData.get('damageType') != "蜂窝芯子损伤":
            descriptData.pop('蜂窝芯子修补层', None)
            data.pop('honeycombCoreRepair', None)
  
        for v, itemList in descriptData.items():
            if isinstance(itemList, list):
                for item in itemList:
                    if item.get("材料"):
                        material = item.get("材料")
                        mType = material.get('type')
                        if (mType == '复合材料单层板'):
                            for itemData in self.compositePlyData:
                                if itemData.get('id') == material.get('id'):
                                    item['材料'] = itemData  
                        elif (mType == '蜂窝芯子'):
                            for itemData in self.honeycombCoreData:
                                if itemData.get('id') == material.get('id'):
                                    item['材料'] = itemData
                        elif (mType == '胶层'):
                            for itemData in self.adhesiveLayerData:
                                if itemData.get('id') == material.get('id'):
                                    item['材料'] = itemData            
            elif isinstance(itemList, dict):
                if itemList.get("材料"):
                    material = itemList.get("材料")
                    mType = material.get('type')
                    if (mType == '复合材料单层板'):
                        for itemData in self.compositePlyData:
                            if itemData.get('id') == material.get('id'):
                                itemList['材料'] = itemData
                    elif (mType == '蜂窝芯子'):
                        for itemData in self.honeycombCoreData:
                            if itemData.get('id') == material.get('id'):
                                itemList['材料'] = itemData
                    elif (mType == '胶层'):
                        for itemData in self.adhesiveLayerData:
                            if itemData.get('id') == material.get('id'):
                                itemList['材料'] = itemData
                            
            
        
        if not self.check_empty_values(descriptData['上铺层']):
            return envs.MBox.warning("请补全上铺层数据")
        if not self.check_empty_values(descriptData['下铺层']):
            return envs.MBox.warning("请补全下铺层数据")
        if not self.check_empty_values(descriptData['上铺层修补层']):
            return envs.MBox.warning("请补全上铺层修补层数据")
        if not self.check_empty_values(descriptData['下铺层修补层']):
            return envs.MBox.warning("请补全下铺层修补层数据")
        if not self.check_empty_values(descriptData['下铺层填充层']):
            return envs.MBox.warning("请补全下铺层填充层数据")
        if not self.widgetDict['honeycombCoreMateial'].getValue():
            return envs.MBox.warning("请补全蜂窝芯子数据")
        if (self.paramsModeingData.get('damageType') == "贯穿式损伤" or  self.paramsModeingData.get('damageType') == "蜂窝芯子损伤") and not self.widgetDict['honeycombCoreRepairMateial'].getValue():
            return envs.MBox.warning("请补全蜂窝芯子修补层数据")
        
        # if (self.component.gridWidgets['damageType'].currentText() == "贯穿式损伤") and not self.check_empty_values(descriptionRes['下铺层修补层']) :
        #     return envs.MBox.warning("请补全下铺层修补层数据")
        
        # print(descriptData)
        file = "data/ipimas/uis/MaterialSetting.json"
        Utils.write_json(file, data)
        self.close()
        filtered_data = remove_id(descriptData)
        
        if "蜂窝芯子修补层" in filtered_data:
            filtered_data["蜂窝芯子修补层"] = filtered_data["蜂窝芯子修补层"].get("材料")
        if "蜂窝芯子材料" in filtered_data:
            filtered_data["蜂窝芯子材料"] = filtered_data["蜂窝芯子材料"].get("材料")
        
        self.materialSetting_save_signal.emit(filtered_data)

        
    def getTableValue(self, table):
        data = []
        descriptData = []
        for row in range(table.rowCount()):
            rowData = {}
            descriptRowData = {}
            for column in range(table.columnCount()):
                if column < 1:
                    wrapWidget = table.cellWidget(row, column)
                    wrapLayout = wrapWidget.layout()
                    selectBox = wrapLayout.itemAt(0).widget()
                    # if column == 0:
                    rowData['material'] = selectBox.getValue()
                    descriptRowData['材料'] = selectBox.getValue()
                    # elif column == 1:
                    #     # rowData['material'] = 'selectBox.getValue()'
                    #     rowData['material'] = selectBox.getValue()
                else:
                    item = table.item(row, column)
                    if item is not None:
                        # print("item.text()=", item.text())
                        rowData['Angle'] = toFloat(item.text())
                        descriptRowData['铺层角度'] = toFloat(item.text())
            data.append(rowData)
            descriptData.append(descriptRowData)
        return [data,descriptData]
        # print(data)

    def setTableSelect(self, table):
        # data = []
        # descriptData = []
        for row in range(table.rowCount()):
            # rowData = {}
            # descriptRowData = {}
            for column in range(table.columnCount()):
                if column == 0:
                    wrapWidget = table.cellWidget(row, column)
                    wrapLayout = wrapWidget.layout()
                    selectBox = wrapLayout.itemAt(0).widget()
                    selectBox.updateItem(self.materialList)
                    # if column == 0:
                    # rowData['material'] = selectBox.getValue()
                    # descriptRowData['材料'] = selectBox.getValue()
                    # elif column == 1:
                    #     # rowData['material'] = 'selectBox.getValue()'
                    #     rowData['material'] = selectBox.getValue()
                
        #     data.append(rowData)
        #     descriptData.append(descriptRowData)
        # return [data,descriptData]
    
    def check_empty_values(self, data_list):
        for item in data_list:
            for key, value in item.items():
                if value == "" or value is None:
                    return False
                #     print(f"属性 '{key}' 的值为空或空字符串")
                # else:
                #     print(f"属性 '{key}' 的值为 '{value}'")    
        return True

        
    def closeEvent(self, event):
        DlgMaterialSetting._shown = False  # 使用类名访问类属性
        DlgMaterialSetting._instance = None  # 清除实例引用
        event.accept()
 
def remove_id(data):
    if isinstance(data, dict):
        return {k: remove_id(v) for k, v in data.items() if k != 'id'}
    elif isinstance(data, list):
        return [remove_id(item) for item in data]
    else:
        return data        
        
def toFloat(value):
    try:
        return float(value)
    except ValueError:
        return None
        
def transform(data1,data2,data3):
    result1 = []
    result2 = []
    result3 = []
    for item in data1:
        # 获取字典的第一个键作为title
        title_key = next(iter(item), None)
        if title_key:
            result1.append({
                "value":{"id":item['id'],"type":"复合材料单层板"},
                "title": item[title_key]
                # "title": "复合材料单层板-" + item[title_key]
            })
    for item in data2:
        # 获取字典的第一个键作为title
        title_key = next(iter(item), None)
        if title_key:
            result2.append({
                "value": {"id":item['id'],"type":"蜂窝芯子"},
                "title": item[title_key]
                # "title": "蜂窝芯子-" + item[title_key]
            })
    for item in data3:
        # 获取字典的第一个键作为title
        title_key = next(iter(item), None)
        if title_key:
            result3.append({
                "value": {"id":item['id'],"type":"胶层"},
                "title": item[title_key]
                # "title": "胶层-" + item[title_key]
            })
    return result1+result2+result3